package com.ycsj.goldmedalnewconcept.fragment;


import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imkit.model.UIConversation;
import io.rong.imkit.widget.adapter.ConversationListAdapter;
import io.rong.imlib.model.Conversation;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.R;

/**
 * 消息 fragment
 * @author dewyze
 *
 */
public class ＭessageFragment extends BaseFragment  {

	private static final String TAG = "MssageFragment";
	private Activity mActivity;
	private Object mConversitionListFragment;
	private FragmentManager mFragmentManager;
	private RelativeLayout mBack;
	private TextView mTitle;

	public static ＭessageFragment newInstance() {
		ＭessageFragment settingFragment = new ＭessageFragment();

		return settingFragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mActivity = activity;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.conversationlist, container,
				false);
		SPUtils.put(getActivity(), ConfigConstant.CURRENTFRAGMENT, TAG);
//		//鍚姩浼氳瘽鍒楄〃鐣岄潰
//		if (RongIM.getInstance() != null)
//		                   RongIM.getInstance().startConversationList(getActivity());
		
//		 isReconnect();
		
		setActionBarTitle(view) ;
	
//			reconnect(SPUtils.getString(getActivity(), ConfigConstant.IMTOKEN));
		
		if(SPUtils.getBoolean(getActivity(), ConfigConstant.IS_LOGIN_ON)){
			
//		Toast.makeText(getActivity(), "fl_message", 0).show();
			
		FragmentTransaction ft = getFragmentManager().beginTransaction();
		
            Bundle args=new Bundle();
			args.putInt("GRADE_ID", 2001);
//			wf.setArguments(args);
			ft.replace(R.id.fl_message, initConversitionList());
			ft.addToBackStack(null);
			ft.commit();
			}else{
				FragmentTransaction ft = getFragmentManager().beginTransaction();
				
	            Bundle args=new Bundle();
				args.putInt("GRADE_ID", 2001);
//				wf.setArguments(args);
				ft.replace(R.id.fl_message,ＭessageNotLoginFragment.newInstance());
				ft.addToBackStack(null);
				ft.commit();
			}
		
//		}else{
//			reconnect(SPUtils.getString(getActivity(), ConfigConstant.IMTOKEN));
//			FragmentTransaction ft = getFragmentManager().beginTransaction();
////			Toast.makeText(getActivity(), "reconnect", 0).show();
//            Bundle args=new Bundle();
//			args.putInt("GRADE_ID", 2001);
////			wf.setArguments(args);
//			ft.replace(R.id.fl_message, initConversitionList());
//			ft.addToBackStack(null);
//			ft.commit();
//		}
		return view;
	}

	
	private Fragment initConversitionList() {
		 ConversationListFragment fragment = ConversationListFragment.getInstance();
		 fragment.setAdapter(new ConversationListAdapterEx(getActivity()));
	        Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
	                .appendPath("conversationlist")
	                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //璁剧疆绉佽亰浼氳瘽闈炶仛鍚堟樉绀�
	                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")//璁剧疆缇ょ粍浼氳瘽闈炶仛鍚堟樉绀�
	                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//璁剧疆璁ㄨ缁勪細璇濋潪鑱氬悎鏄剧ず
	                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//璁剧疆绯荤粺浼氳瘽闈炶仛鍚堟樉绀�
	                .build();
	        fragment.setUri(uri);
	        
	        return fragment;
	}
	
	
	
	
	
	

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initViews(view);
		initEvents();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
	}

	private void initViews(View view) {
		
	}
	
	private void initEvents() {
		
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
//		ConversationListFragment.getInstance().onDestroy();
//		Log.e("test", "onDestroy+message");
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	public String getFragmentName() {
		return TAG;
	}


	
	 /**
     * 鍔犺浇 浼氳瘽鍒楄〃 ConversationListFragment
     */
    @SuppressLint("NewApi")
	private void enterFragment() {

      //  ConversationListFragment fragment = (ConversationListFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.conversationlist);

//        Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
//                .appendPath("conversationlist")
//                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false") //璁剧疆绉佽亰浼氳瘽闈炶仛鍚堟樉绀�
//                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "true")//璁剧疆缇ょ粍浼氳瘽鑱氬悎鏄剧ず
//                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")//璁剧疆璁ㄨ缁勪細璇濋潪鑱氬悎鏄剧ず
//                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")//璁剧疆绯荤粺浼氳瘽闈炶仛鍚堟樉绀�
//                .build();
//
//        fragment.setUri(uri);
        
      
        
    }

    /**
     * 璁剧疆 actionbar 浜嬩欢
     * @param view 
     */
    private void setActionBarTitle(View view) {

        mTitle = (TextView) view.findViewById(R.id.tv_title);
        mBack = (RelativeLayout) view.findViewById(R.id.rl_back);

        mTitle.setText("消息");
        mBack.setVisibility(View.GONE);
//        mBack.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//            	Toast.makeText(getActivity(), TAG, 0).show();
//            }
//        });

    }

    /**
     * 鍒ゆ柇娑堟伅鏄惁鏄� push 娑堟伅
     *
     */

    /**
     * 閲嶈繛
     *
     * @param token
     */
    @SuppressLint("NewApi")
	
    public class ConversationListAdapterEx extends ConversationListAdapter {
        public ConversationListAdapterEx(Context context) {
            super(context);
        }

        @Override
        protected View newView(Context context, int position, ViewGroup group) {
        	
            return super.newView(context, position, group);
        }

        @Override
        protected void bindView(View v, int position, UIConversation data) {
        if(data.getConversationType().equals(Conversation.ConversationType.DISCUSSION))
            data.setUnreadType(UIConversation.UnreadRemindType.REMIND_ONLY);
            super.bindView(v, position, data);
        }
    }
	
    
 
}
